package com.nf.bookstytem.dao.bookmanage;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.nf.bookstytem.model.bookmanage.BookModel;
import com.nf.bookstytem.uitl.QueryRunnerWrapper;

public class BookManageDaoImpl {
	public List<BookModel> getAll(){
		String sql="select * from books_information order by biid";
		List<BookModel> books=QueryRunnerWrapper.query(sql, new BeanListHandler<BookModel>(BookModel.class));
		return books;
	}
	
	public int deleteBookById(String id) {
		String sql="delete from books_information where bibookid=?";
		int n=QueryRunnerWrapper.update(sql, id);
		return n;
	}
	
	public int insertBook(BookModel book) {
		String sql="insert into books_information values(?,?,?,?,?,?,?)";
		int n=QueryRunnerWrapper.update(sql,book.getBibookid(),book.getBiname(),book.getBiauthor(),
				book.getBinum(),book.getBitype(),book.getBipress(),book.getBibookstatus());
		return n; 
	}
	
	public int updateProduct(BookModel book) {
		String sql="update books_information set biname=?,biauthor=?,binum=?,bitype=?,bipress=?,bibookstatus=? where bibookid=?";
		int n=QueryRunnerWrapper.update(sql, book.getBiname(),book.getBiauthor(),book.getBinum(),
				book.getBitype(),book.getBipress(),book.getBibookstatus(),book.getBibookid());
		return n;
	}
	 public List<BookModel> getLikeListByName(String biname){
		String sql="select * from books_information where biname like '%"+biname+"%'";
		List<BookModel> books=QueryRunnerWrapper.query(sql, new BeanListHandler<BookModel>(BookModel.class));
		return books;
	}
	 public List<BookModel> PageList(int pageNo,int pageSize){
			String sql="select * from (select ROW_NUMBER()over(order by bibookid)rowNo,* from books_information)t where RowNo between "+((pageNo-1)*pageSize+1)+" and "+(pageNo*pageSize)+"";
			List<BookModel> list=QueryRunnerWrapper.query(sql,  new BeanListHandler<BookModel>(BookModel.class));
			return list;
		}
		
	public int getPages() {
		String sql="select count(*) from books_information";
		int n=QueryRunnerWrapper.queryCount(sql, new ScalarHandler<>(1));
		return n;
	}

	public static void main(String[] args) {
		BookManageDaoImpl dao=new BookManageDaoImpl();int pageNo=2;
		int pagesize=3;
		System.out .println(dao.PageList(pageNo,pagesize));
		}
}
